要在Java中製作表單,你可以使用Java的Swing庫來創建圖形用戶界面(GUI)。以下是基本步驟和程式碼範例,教你如何開始製作一個簡單的表單:
你需要導入Java Swing和AWT包來創建GUI元件。
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
JFrame 是Swing的頂層容器,用來顯示視窗。
public class SimpleForm extends JFrame {
public SimpleForm() {
setTitle("Simple Form");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(3, 2)); // 設置表單為網格佈局
}
}
你可以使用 JLabel、JTextField、JButton 等元件來創建表單。
public class SimpleForm extends JFrame {
public SimpleForm() {
setTitle("Simple Form");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(3, 2)); // 3行2列的網格佈局
// 添加表單元件
JLabel nameLabel = new JLabel("Name:");
JTextField nameField = new JTextField();
JLabel emailLabel = new JLabel("Email:");
JTextField emailField = new JTextField();
JButton submitButton = new JButton("Submit");
// 將元件添加到JFrame
add(nameLabel);
add(nameField);
add(emailLabel);
add(emailField);
add(submitButton);
}
}
你可以使用ActionListener來處理按鈕的點擊事件。
public class SimpleForm extends JFrame {
public SimpleForm() {
setTitle("Simple Form");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(3, 2));
JLabel nameLabel = new JLabel("Name:");
JTextField nameField = new JTextField();
JLabel emailLabel = new JLabel("Email:");
JTextField emailField = new JTextField();
JButton submitButton = new JButton("Submit");
add(nameLabel);
add(nameField);
add(emailLabel);
add(emailField);
add(submitButton);
// 添加按鈕的事件處理
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String email = emailField.getText();
JOptionPane.showMessageDialog(null, "Name: " + name + "\nEmail: " + email);
}
});
}
public static void main(String[] args) {
// 顯示表單
SimpleForm form = new SimpleForm();
form.setVisible(true);
}
}
在 main 方法中創建 SimpleForm 的實例並設置為可見。
程式碼完整範例
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SimpleForm extends JFrame {
public SimpleForm() {
setTitle("Simple Form");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(3, 2));
JLabel nameLabel = new JLabel("Name:");
JTextField nameField = new JTextField();
JLabel emailLabel = new JLabel("Email:");
JTextField emailField = new JTextField();
JButton submitButton = new JButton("Submit");
add(nameLabel);
add(nameField);
add(emailLabel);
add(emailField);
add(submitButton);
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String email = emailField.getText();
JOptionPane.showMessageDialog(null, "Name: " + name + "\nEmail: " + email);
}
});
}
public static void main(String[] args) {
SimpleForm form = new SimpleForm();
form.setVisible(true);
}
}
執行這段程式碼後,你會看到一個簡單的表單,允許用戶輸入姓名和電子郵件,並在點擊「Submit」按鈕後顯示輸入的信息。